package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.base.result.ResponseVo;
import com.atguigu.srb.core.pojo.entity.LendItem;
import com.atguigu.srb.core.pojo.vo.InvestVO;
import com.atguigu.srb.core.service.LendItemService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标的出借记录表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-03-25
 */
@RestController
@RequestMapping("/api/core/lendItem")
public class ApiLendItemController {
    @Resource
    LendItemService lendItemService;

    @ApiOperation("查询标的的投资列表")
    @GetMapping("list/{lendId}")
    public ResponseVo listByLendId(@PathVariable("lendId")Long lendId){
        List<LendItem> items = lendItemService.list(Wrappers.lambdaQuery(LendItem.class)
                .eq(LendItem::getLendId, lendId)
                .orderByDesc(LendItem::getUpdateTime));
        return ResponseVo.ok().data("items",items);

    }

    @ApiOperation("查询投资人投资记录")
    @GetMapping("{pageNum}/{pageSize}")
    public ResponseVo list(@RequestHeader(value = "token",required = false)String token,
                           @PathVariable("pageNum")Integer pageNum,
                           @PathVariable("pageSize")Integer pageSize){
        Page<LendItem> page = lendItemService.investList(token,pageNum,pageSize);
        return ResponseVo.ok().data("page",page);
    }
    //http://192.168.1.171/api/core/lendItem/notify
    @ApiOperation("投资回调接口：投资人在hfb中操作投资之后hfb的回调")
    @PostMapping("notify")
    public String investNotify(@RequestParam Map<String,Object> map){
        return lendItemService.investNotify(map);
    }
    @ApiOperation("投资")
    @PostMapping("invest")
    public ResponseVo invest(@RequestBody InvestVO investVO,
                             @RequestHeader(value = "token",required = false)String token){
        //返回自动提交的表单字符串
        String  form = lendItemService.invest(investVO,token);
        return ResponseVo.ok().data("form",form);
    }
}

